class Camera {
	/**
	 * @description: Camera 
	 * @param {Object} viewer {Cesium.Viewer}
	 */
	constructor(viewer) {
		this._viewer = viewer;
		this._camera = viewer.camera;
	}

	/**
	 * @description 获取相机位置
	 */
	get() {
		return {
			x: this._camera.positionCartographic.longitude,
			y: this._camera.positionCartographic.latitude,
			z: this._camera.positionCartographic.height,
			pitch: this._camera.pitch,
			heading: this._camera.heading,
			roll: this._camera.roll
		}
	}
	/**
	 * @description 飞行
	 * @param {Object} option{
		 location:{
			x:坐标X,
			y:坐标Y,
			z:坐标Z 
		 },
		 rotation:{
			heading:偏航角,
			pitch: 俯仰角,
			roll:翻滚角
		 },
		 time:飞行时间
		 complete:飞行结束后执行的功能。
	 }
	 */
	fly(option) {
		let options = Cesium.defaultValue(option, {});
		let param = {
			destination: Cesium.Cartesian3.fromDegrees(options.location.x, options.location.y, options.location.z)
		};
		let orientation = {};
		if(Cesium.defined(options.rotation)){
			if (Cesium.defined(options.rotation.heading)) {
				orientation.heading = Cesium.Math.toRadians(options.rotation.heading)
			}
			if (Cesium.defined(options.rotation.pitch)) {
				orientation.pitch = Cesium.Math.toRadians(options.rotation.pitch)
			}
			if (Cesium.defined(options.rotation.roll)) {
				orientation.roll = Cesium.Math.toRadians(options.rotation.roll)
			}
			
			if (Cesium.defined(options.rotation.heading) || Cesium.defined(options.rotation.pitch) || Cesium.defined(options.rotation.roll)) {
				param.orientation = orientation;
			}
		}
		if (Cesium.defined(options.time)) {
			param.duration = options.time
		}
		if (Cesium.defined(options.complete)) {
			param.complete = options.complete
		}
		if (Cesium.defined(options.maximumHeight)) {
			param.maximumHeight = options.maximumHeight
		}
		if (Cesium.defined(options.easingFunction)) {
			param.easingFunction = options.easingFunction
		}
		this._camera.flyTo(param);
	}
	/**
	 * @description 前进
	 */
	moveForward(fn) {
		let result = {
			code: 200,
			message: '成功!'
		};
		try {
			this._camera.moveForward()
		} catch (e) {
			result.code = 500;
			result.message = e;
		}
		if (fn) {
			fn(result);
		}
	}

	/**
	 * @description 后退
	 */
	moveBackward(fn) {
		let result = {
			code: 200,
			message: '成功!'
		};
		try {
			this._camera.moveBackward()
		} catch (e) {
			result.code = 500;
			result.message = e;
		}
		if (fn) {
			fn(result);
		}
	}

	/**
	 * @description 左移
	 */
	moveLeft() {
		let result = {
			code: 200,
			message: '成功!'
		};
		try {
			this._camera.moveLeft()
		} catch (e) {
			result.code = 500;
			result.message = e;
		}
		if (fn) {
			fn(result);
		}
	}

	/**
	 *  @description 右移
	 */
	moveRight(fn) {
		let result = {
			code: 200,
			message: '成功!'
		};
		try {
			this._camera.moveRight()
		} catch (e) {
			result.code = 500;
			result.message = e;
		}
		if (fn) {
			fn(result);
		}
	}

	/**
	 * @description 上升
	 */
	moveUp(fn) {
		let result = {
			code: 200,
			message: '成功!'
		};
		try {
			this._camera.moveUp()
		} catch (e) {
			result.code = 500;
			result.message = e;
		}
		if (fn) {
			fn(result);
		}
	}

	/**
	 * @description 下降
	 */
	moveDown(fn) {
		let result = {
			code: 200,
			message: '成功!'
		};
		try {
			this._camera.moveDown()
		} catch (e) {
			result.code = 500;
			result.message = e;
		}
		if (fn) {
			fn(result);
		}
	}

	/**
	 * @description 左转
	 */
	turnLeft(fn) {
		let result = {
			code: 200,
			message: '成功!'
		};
		try {
			this._camera.rotateLeft()
		} catch (e) {
			result.code = 500;
			result.message = e;
		}
		if (fn) {
			fn(result);
		}
	}

	/**
	 * @description 右转
	 */
	turnRight(fn) {
		let result = {
			code: 200,
			message: '成功!'
		};
		try {
			this._camera.rotateRight()
		} catch (e) {
			result.code = 500;
			result.message = e;
		}
		if (fn) {
			fn(result);
		}
	}

	/**
	 * @description 抬头
	 */
	turnUp(fn) {
		let result = {
			code: 200,
			message: '成功!'
		};
		try {
			this._camera.rotateUp()
		} catch (e) {
			result.code = 500;
			result.message = e;
		}
		if (fn) {
			fn(result);
		}
	}

	/**
	 *@description 低头
	 */
	turnDown(fn) {
		let result = {
			code: 200,
			message: '成功!'
		};
		try {
			this._camera.rotateDown()
		} catch (e) {
			result.code = 500;
			result.message = e;
		}
		if (fn) {
			fn(result);
		}
	}

	/**
	 *@description 取消相机移动
	 */
	cancel(fn) {
		let result = {
			code: 200,
			message: '成功!'
		};
		try {
			this._camera.cancelFlight();
		} catch (e) {
			result.code = 500;
			result.message = e;
		}
		if (fn) {
			fn(result);
		}
	}
	
	/**
	 * @description 监听
	 * @param {Object} type : moveStart- 开始移动  moveEnd- 停止移动  changed- 相机更改
	 * @param {Object} fn
	 */
	addEventListener(type,fn){
		if('moveStart'==type){
			this.moveStart=fn;
			this._camera.moveStart.addEventListener(this.moveStart, this);
		}
		else if('moveEnd'==type){
			this.moveEnd=fn;
			this._camera.moveEnd.addEventListener(this.moveEnd, this);
		}
		else if('changed'==type){
			this.changed=fn;
			this._camera.changed.addEventListener(this.changed, this);
		}
	}
	
	/**
	 * @description 移出监听
	 * @param {Object} type : moveStart- 开始移动  moveEnd- 停止移动  changed- 相机更改
	 */
	removeEventListener(type){
		if('moveStart'==type){
			this._camera.moveStart.removeEventListener(this.moveStart, this);
			this.moveStart=null;
		}
		else if('moveEnd'==type){
			this._camera.moveEnd.removeEventListener(this.moveEnd, this);
			this.moveEnd=null;
		}
		else if('changed'==type){
			this._camera.changed.removeEventListener(this.changed, this);
			this.changed=null;
		}
	}
	
	/**
	 * @description 相机根据时间和角度旋转(自身旋转)
	 * @param {Object} param{
		angle:旋转角度,
		time:旋转总时间,
		clockwise:是否顺时针 默认true 如果是false则逆时针旋转(true/false),
		complete:回调函数
	 }
	 */
	rotateByTime(param){
		
	}
	
	/**
	 * @description 地球上根据目标点 时间 旋转角度绕点旋转
	 * @param {Object} param{
		target:目标点世界坐标, 
		angle:旋转角度 单位 角度, 
		time:旋转总时间 单位 毫秒, 
		disablePick:是否关闭pick 默认true, 
		clockwise:是否顺时针 默认true 如果是false则逆时针旋转, 
		complete:回调函数, 
	 }
	 */
	flyRotateByTime(param){
		
	}
	
	/**
	 * @description 地球上根据目标点 时间 旋转角度绕点旋转
	 * @param {Object} param{
		speed:旋转速度,
		target:目标点世界坐标,
		angle:要旋转的角度,
		disablePick:是否关闭pick 默认true,
		clockwise:是否顺时针 默认true 如果是false则逆时针旋转,
		complete:回调函数,
	 }
	 */
	flyRotateBySpeed(param){
		
	}
	
	/**
	 * @description 销毁
	 */
	destroy(){
		if(this.moveStart){
			this._camera.moveStart.removeEventListener(this.moveStart, this);
		}
		if(this.moveEnd){
			this._camera.moveEnd.removeEventListener(this.moveEnd, this);
		}
		if(this.changed){
			this._camera.changed.removeEventListener(this.changed, this);
		}
		delete this.moveStart
		delete this.moveEnd
		delete this.changed
		delete this._camera
		delete this._viewer
		return Cesium.destroyObject(this);
	}
}

export {
	Camera
};
